﻿using CodeNotes.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcCodeNotes
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            //定时访问自己的页面，保证网站不会关闭
            string strUrl = ConfigurationManager.AppSettings["HostUrl"];            
            if (strUrl != "")
            {
                string IntervalMins = ConfigurationManager.AppSettings["IntervalMins"];
                var aTimer = new System.Timers.Timer();
                // Hook up the Elapsed event for the timer.
                aTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimedEvent);
                // Set the Interval to 30 seconds (30000 milliseconds).                
                aTimer.Interval = Convert.ToInt32(IntervalMins) * 60 * 1000;
                aTimer.AutoReset = true;//设置是执行一次（false）还是一直执行(true)； 
                aTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                //aTimer.Start();                
            }
         
        }

        public void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {   
         
            string strUrl = ConfigurationManager.AppSettings["HostUrl"];            
            System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
            System.Net.HttpWebResponse _HttpWebResponse = (System.Net.HttpWebResponse)_HttpWebRequest.GetResponse();
            System.IO.Stream _Stream = _HttpWebResponse.GetResponseStream();//得到回写的字节流
        }

        protected void Application_Error()
        {
            //// 在出现未处理的错误时运行，获取错误
            if (ConfigurationManager.AppSettings.Get("errorLog") == "Yes")
            {
                Exception objErr = Server.GetLastError().GetBaseException();
                string error = string.Empty;
                if (objErr != null)
                {
                    //error = "Error Page: " + Request.Url.ToString() + "/n";
                    if (objErr.Message != null)
                    {
                        error += "Error Message: " + objErr.Message + "/n";
                    }
                    if (objErr.StackTrace != null)
                    {
                        error += "Stack Message:" + objErr.StackTrace + "/n";
                    }
                    if (objErr.InnerException != null)
                    {
                        error += "InnerException" + objErr.InnerException.Message + "/n";
                    }
                }

                Log.LogNote("系统报错", error, "error");

                Response.Clear();
                Response.Write("A technical issue has arisen and an email has been sent to technical support regarding this.");
                Response.Write("<br />");
                Response.Write("<br />");
                Response.Write("To try again, please click the back button in your web browser and try to reprocess your query.");

                Response.End();
            }
            
        }
    }
}